package com.schtkc.fishingmaster.config;

import com.google.common.base.Predicate;
import com.schtkc.fishingmaster.utils.Constants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * SwaggerConfig
 *
 * @Author: ydz
 * @CreateTime: 2022-06-03  21:17
 */
@EnableSwagger2
@Configuration
public class AdminSwaggerConfig implements WebMvcConfigurer {
//    private final Predicate<RequestHandler> pkdApp = RequestHandlerSelectors.basePackage("com.buyoute.pocketsh.controllerApp");
    private final Predicate<RequestHandler> pkgAdmin = RequestHandlerSelectors.basePackage("com.schtkc.fishingmaster.controller");

    @Bean
    public Docket docketAdmin(Environment environment) {
        //设置在哪些环境中显示swagger
//        Profiles profiles = Profiles.of("kdsh", "test", "local");
//        boolean showSwagger = environment.acceptsProfiles(profiles);
//        System.out.println("app是否显示swagger:" + showSwagger);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo("admin"))
                .groupName("admin")
                .enable(true)
                .select()
                .apis(pkgAdmin)
//                .apis(Predicates.or(pkgAdmin, pkdApp))
//                .apis(RequestHandlerSelectors.basePackage("com.buyoute.pocketsh.controllerAdmin"))
                //路径过滤
//                .paths(PathSelectors.ant("/config/**"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey(Constants.TOKEN_KEY, Constants.TOKEN_KEY, "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(Constants.TOKEN_KEY, authorizationScopes));
        return securityReferences;
    }


    private ApiInfo apiInfo(String key) {
        Contact contact = new Contact("Souja", "https://baike.baidu.com/item/soulja/10406427?fr=aladdin", "782579195@qq.com");
        return new ApiInfo("休闲钓鱼大师" + key + " Api文档",
                "阿斯蒂芬",
                "v1.0",
                "http://kdsh.buyoute.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
